package org.cnccc.elem;

import java.util.ArrayList;

public class Element {
	private String code;
	private String labelName;
	private boolean single;
	private ArrayList<Element> subElements = new ArrayList<Element>();
	private String id;
	private String name;
	private String className;
	private String style;
	private String attributes;
	
	private boolean hasExAttrs;
	
	private String parentCode;
	private String DataCode;
	
	
	public String getParentCode() {
		return parentCode;
	}

	public void setParentCode(String parentCode) {
		this.parentCode = parentCode;
	}

	public String getDataCode() {
		return DataCode;
	}

	public void setDataCode(String dataCode) {
		DataCode = dataCode;
	}

	public boolean hasExAttr(){
		return hasExAttrs;
	}
	
	public boolean isHasExAttrs() {
		return hasExAttrs;
	}
	public void setHasExAttrs(boolean hasExAttrs) {
		this.hasExAttrs = hasExAttrs;
	}
	public Attribute[] getExAttrs() {
		return exAttrs;
	}
	public void setExAttrs(Attribute[] exAttrs) {
		this.exAttrs = exAttrs;
	}

	private Attribute[] exAttrs;
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getLabelName() {
		return labelName;
	}
	public void setLabelName(String labelName) {
		this.labelName = labelName;
	}
	
	public boolean isSingle() {
		return single;
	}
	public void setSingle(boolean single) {
		this.single = single;
	}
	
	public ArrayList<Element> getSubElements() {
		return subElements;
	}

	public void setSubElements(ArrayList<Element> subElements) {
		this.subElements = subElements;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public String getAttributes() {
		return attributes;
	}
	public void setAttributes(String attributes) {
		this.attributes = attributes;
	}
	
	public String toString(){
		StringBuffer ret = new StringBuffer("<");
		ret.append(labelName);
		if(id != null && !"".equals(id.trim())){
			ret.append(" id=\"");
			ret.append(id);
			ret.append("\"");
		}
		if(name != null && !"".equals(name.trim())){
			ret.append(" name=\"");
			ret.append(name);
			ret.append("\"");
		}
		if(className != null && !"".equals(className.trim())){
			ret.append(" class=\"");
			ret.append(className);
			ret.append("\"");
		}
		if(style != null && !"".equals(style.trim())){
			ret.append(" style=\"");
			ret.append(style);
			ret.append("\"");
		}
		if(attributes != null && !"".equals(attributes.trim())){
			ret.append(" ");
			ret.append(attributes);
		}
		if(hasExAttrs && exAttrs != null){
			for (int j = 0; j < exAttrs.length; j++) {
				Attribute attr = exAttrs[j];
				if(attr == null) continue;
				ret.append(" ");
				ret.append(attr.name);
				ret.append("=\"");
				ret.append(attr.value);
				ret.append("\"");
			}
		}
		
		if(single){
			ret.append("/>");
		} else {
			ret.append(">\n");
			if(subElements != null)
			for (int i = 0; i < subElements.size(); i++) {
				Element e = subElements.get(i);
				if(e == null ) continue;
				ret.append(e.toString());
			}
			ret.append("</");
			ret.append(labelName);
			ret.append(">\n");
		}
		return ret.toString();
	}
	public boolean isRoot(){
		return "00".equals(parentCode);
	}
	public void addChild(Element e) {
		subElements.add(e);

	}
}
